package com.java110.code;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.code.newBack.BackCoderGeneratorStart;
import com.java110.code.newBack.BaseGenerator;
import com.java110.utils.util.StringUtil;
import java.io.IOException;
import java.sql.SQLException;

public class TableToJson {
    // "   \n" +
    //show create table c_orders  用这个语句获取


    public static final String createTableSql = "CREATE TABLE `gov_case` (\n" +
            "  `case_id` varchar(30) NOT NULL COMMENT '案件ID',\n" +
            "  `gov_victim_id` varchar(30) NOT NULL COMMENT '受害人ID',\n" +
            "  `victim_name` varchar(64) NOT NULL COMMENT '受害人名称',\n" +
            "  `case_name` varchar(64) NOT NULL COMMENT '案件名称',\n" +
            "  `ca_id` varchar(30) NOT NULL COMMENT '区域ID',\n" +
            "  `case_num` varchar(32) NOT NULL COMMENT '案件编号',\n" +
            "  `happen_time` datetime NOT NULL COMMENT '发生日期',\n" +
            "  `gov_suspect_id` varchar(30) NOT NULL COMMENT '嫌疑人ID',\n" +
            "  `suspect_name` varchar(64) NOT NULL COMMENT '嫌疑人名称',\n" +
            "  `settle_time` datetime NOT NULL COMMENT '结案日期',\n" +
            "  `brief_condition` varchar(1024) DEFAULT NULL COMMENT '简要情况',\n" +
            "  `ramark` varchar(1024) DEFAULT NULL COMMENT '备注',\n" +
            "  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
            "  `status_cd` varchar(2) NOT NULL DEFAULT '0' COMMENT '数据状态，详细参考c_status表，0在用，1失效',\n" +
            "  PRIMARY KEY (`case_id`)\n" +
            ")";

    public static void main(String[] args) throws Exception {

        String outFilePatch = "/template_1.json";
        String desc = "命案基本信息";
        String id = "caseId";
        String name = "govCase";
        String shareName = "gov";
        String shareColumn = "ca_id";
        String shareParam = "caId";

        //业务名称 desc 业务编码名称生成后类名 name 主键 id  需要放到那个服务 shareName
        String newSql = createTableSql.substring( createTableSql.indexOf( "(" ) + 1, createTableSql.lastIndexOf( ")" ) );
        String tableName = createTableSql.substring( createTableSql.indexOf( "TABLE" ) + 5, createTableSql.indexOf( "(" ) );
        tableName = tableName.replaceAll( "`", "" ).trim();
        newSql = newSql.replaceAll( "\n", "" );
        String[] rowSqls = newSql.split( "'," );
        JSONObject param = new JSONObject();
        param.put( "autoMove", true );
        param.put( "desc", desc );//描述
        param.put( "id", id ); //主键
        param.put( "name", name ); // 表名大写
        param.put( "shareColumn", shareColumn ); //分片字段
        param.put( "shareName", shareName ); //生成到那个服务下
        param.put( "shareParam", shareParam ); //分片字段大写
        param.put( "tableName", tableName ); //表名
        JSONObject paramColumn = new JSONObject();
        JSONArray requireds = new JSONArray();
        JSONObject required = null;
        String key = "";
        for (String rowSql : rowSqls) {
            required = new JSONObject();
            key = rowSql.trim();
            key = key.substring( 0, key.indexOf( " " ) );
            key = key.replaceAll( "`", "" );
            if ("UNIQUE".equals( key )) {
                continue;
            }
            if ("PRIMARY".equals( key )) {
                continue;
            }
            if ("b_id".equals( key )) {
                continue;
            }
            if ("create_time".equals( key )) {
                continue;
            }
//            if ("status_cd".equals(key)) {
//                continue;
//            }
            if (rowSql.toLowerCase().contains( "not null" )) {
                if (!"status_cd".equals( key )) {
                    required.put( "code", StringUtil.lineToHump( key ) );
                    String comment = rowSql.contains( "COMMENT" ) ? rowSql.substring( rowSql.indexOf( "COMMENT '" ) + 9 ) : StringUtil.lineToHump( key );
                    comment = comment.trim();
                    if (comment.contains( "，" )) {
                        comment = comment.split( "，" )[0];
                    }
                    if (comment.contains( " " )) {
                        comment = comment.split( " " )[0];
                    }

                    required.put( "msg", comment + "不能为空" );
                    requireds.add( required );
                }

            }
            paramColumn.put( StringUtil.lineToHump( key ), key );
        }
        param.put( "param", paramColumn );
        param.put( "required", requireds );
        System.out.println( param.toJSONString() );
        JSONArray str = new JSONArray(  );
        str.add( outFilePatch );
        BaseGenerator.writeFile(TableToJson.class.getResource("/").getPath()
                + "newBack/"+outFilePatch,param.toJSONString());

        BackCoderGeneratorStart.main( str.toArray(new String[str.size()]) );
    }

}
